﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Login
{
    public partial class btnCUD : Form
    {
        List<int> lstInt = new List<int>();
        List<double> lstDouble = new List<double> { 5.0, 8.89, 9 };
        public btnCUD()
        {
            InitializeComponent();
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            lstInt = new List<int> { 1, 2, 5, 6, 6, 8, 9, 10 };
        }

        private void btnLoop_Click(object sender, EventArgs e)
        {
            lbForeach.Items.Clear();
            foreach (int i in lstInt)
            {
                lbForeach.Items.Add(i.ToString());
            }
            lbFor.Items.Clear();
            for (int i = 0; i < lstInt.Count; i++)
            {
                lbFor.Items.Add(lstInt[i].ToString());
            }
        }

        private void btnRetrive_Click(object sender, EventArgs e)
        {
            string strPos = "";
            for(int i = 0; i < lstInt.Count; i++)
            {
                if (lstInt[i].ToString() == tbVal.Text)
                {
                    strPos = strPos + i.ToString() + "   ";
                }
            }
            Text = strPos;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < lstInt.Count; i++)
            {
                if (lstInt[i] == 5)
                {
                    lstInt[i] = 55;
                }
            }

            string strPos = null;
            for (int i = 0; i < lstInt.Count; i++)
            {
                strPos = strPos + lstInt[i].ToString() + "    ";
            }
            Text = strPos;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //错误的方式，遍历时，不能修改、增减元素
            foreach(int i in lstInt)
            {
                if (i == 5)
                {
                    int idx = lstInt.IndexOf(i);
                    lstInt[idx] = 80;
                }
                if (i == 6)
                {
                    lstInt.Remove(i);
                }
                if (i == 7)
                {
                    lstInt.Add(67);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            lstInt.Add(89);
            lstInt.Insert(0, 111);
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            //删除List中值为6的元素
            //lstInt = new List<int> { 1, 2, 5, 6, 6, 8, 9, 10 };
            for (int i = lstInt.Count - 1; i >= 0; i--)
            {
                if (lstInt[i] == 6)
                {
                    lstInt.Remove(lstInt[i]);
                }
            }

            string strPos = null;
            for (int i = 0; i < lstInt.Count; i++)
            {
                strPos = strPos + lstInt[i].ToString() + "    ";
            }
            Text = strPos;
        }
    }
}
